package com.alnex.faceAi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.alnex.faceAi.base.FaceLoginResult;
import com.alnex.faceAi.entity.Result;
import com.alnex.faceAi.entity.ResultCode;
import com.alnex.faceAi.service.FaceLoginService;
import com.alnex.faceAi.utils.BaiduAiUtil;
import com.baidu.aip.util.Base64Util;

/**
 * 刷脸登录测试
 */
@RestController
@RequestMapping("face")
public class FaceController {

    @Autowired
    private FaceLoginService faceLoginService;

    @Autowired
    private BaiduAiUtil baiduAiUtil;

    @RequestMapping("test")
    public void t0() {
        System.out.println("没问题");
    }

    /**
     * 注册
     */
    @RequestMapping("/register/face")
    public Result registerFace(@RequestParam("fid") String fid) {
        return null;
    }
    /**
     * 注册
     */
    @RequestMapping("/update/face")
    public Result updateFace(@RequestParam("fid") String fid) {
        return null;
    }

    /**
     * 获取刷脸登录二维码
     */
    @RequestMapping(value = "/qrcode", method = RequestMethod.GET)
    public Result qrcode() throws Exception {
        return new Result(ResultCode.SUCCESS,faceLoginService.getQRCode());
    }

    /**
     * 检查二维码：登录页面轮询调用此方法，根据唯一标识code判断用户登录情况
     */
    @RequestMapping(value = "/qrcode/{code}", method = RequestMethod.GET)
    public Result qrcodeCeck(@PathVariable(name = "code") String code) throws Exception {
        FaceLoginResult codeCheck = faceLoginService.checkQRCode(code);
        if ("-1".equals(codeCheck.getState())){
            return new Result(ResultCode.SUCCESS,codeCheck);
        }else {
            return new Result(ResultCode.FAIL,codeCheck);
        }
    }

    /**
     * 人脸登录：根据落地页随机拍摄的面部头像进行登录
     * 根据拍摄的图片调用百度云AI进行检索查找
     */
    @RequestMapping(value = "/{code}", method = RequestMethod.POST)
    public Result loginByFace(@PathVariable(name = "code") String code, @RequestParam(name = "file") MultipartFile attachment) throws Exception {
        String userId = faceLoginService.loginByFace(code, attachment);
        if (StringUtils.isEmpty(userId)){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }


    /**
     * 图像检测，判断图片中是否存在面部头像
     */
    @RequestMapping(value = "/checkFace", method = RequestMethod.POST)
    public Result checkFace(@RequestParam(name = "file") MultipartFile attachment) throws Exception {
        if (attachment == null || attachment.isEmpty()){
            return new Result(ResultCode.FAIL,"未检测到面部头像");
        }
        String encode = Base64Util.encode(attachment.getBytes());
        String string = Base64Utils.encodeToString(attachment.getBytes());

        Boolean aBoolean = baiduAiUtil.faceCheck(encode);
        if (aBoolean){
            return new Result(ResultCode.SUCCESS);
        }else {
            return new Result(ResultCode.FAIL);
        }

    }
}
